import { getModelDetailsApi } from '@/api/ai/management'

const useAiStore = defineStore('ai', {
  state: () => ({
    aiInfo: {
      id: null,
      name: null,
      type: null,
      status: null,
      apiKey: null,
      apiUrl: null,
      aiModelConfigs: []
    }
  }),
  actions: {
    /**修改ai模型列表 */
    setAiModelList(list) {
      this.aiInfo.aiModelConfigs = list
    },
    /**清空ai模型列表 */
    removeAiModelList() {
      this.aiInfo.aiModelConfigs = []
    },
    /**删除ai单个模型 */
    removeAiModelItem(name) {
      const _index = this.aiInfo.aiModelConfigs.findIndex((item) => item.name === name)
      this.aiInfo.aiModelConfigs.splice(_index, 1)
    },
    /**获取ai信息 */
    getAiInfo() {
      return new Promise((resolve, reject) => {
        getModelDetailsApi()
          .then((res) => {
            Object.keys(this.aiInfo).forEach((key) => {
              this.aiInfo[key] = res?.data[key]
            })
            resolve()
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    /**修改ai信息 */
    setAiInfo(obj) {
      this.aiInfo = obj
    },
    /**清空ai信息 */
    removeAiInfo() {
      this.aiInfo = {}
    },
    /**删除ai信息key的值 */
    removeAiInfoKey(key) {
      if (key === 'aiModelConfigs') {
        this.aiInfo[key] = []
        return
      }
      this.aiInfo[key] = null
    }
  }
})

export default useAiStore
